package com.uselesstoolkit;

import cn.hutool.http.HttpUtil;
import com.JsonGetter.Getter;
import com.baidu.ai.aip.Ocr;
import com.jdbc.ConnUtil;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Window;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;

public class HelloController implements Initializable {

    @FXML
    private Tab tab3;

    @FXML
    private TextArea jingque2;

    @FXML
    private ImageView jingque1;

    @FXML
    private Tab tab1;

    @FXML
    private Tab tab2;

    @FXML
    private ImageView head;

    @FXML
    private ImageView view;

    @FXML
    private TextArea id_text;

    @FXML
    private TextArea saorao_text;

    @FXML
    private TextArea guishudi_text;

    @FXML
    private TextArea wangluo2;

    @FXML
    private ImageView wangluo1;

    @FXML
    private TextField saorao;

    @FXML
    private ImageView yinhang1;

    @FXML
    private ImageView shouxie1;

    @FXML
    private TextArea yinhang2;

    @FXML
    private TextField id;

    @FXML
    private TextArea shouxie2;

    @FXML
    private ImageView wallpaper;

    @FXML
    private ImageView biaoge1;

    @FXML
    private TextArea biaoge2;

    @FXML
    private ImageView bing;

    @FXML
    private TextField guishudi;

    @FXML
    private TextField domain;

    @FXML
    private ImageView tongyong1;

    @FXML
    private TextArea tongyong2;

    @FXML
    private TextArea domain_text;

    @FXML
    void save_bing(ActionEvent event) {
        Image image=bing.getImage();
        saveImage(event,image);
    }

    @FXML
    void get_head(ActionEvent event) {
        String url1="https://api.vvhan.com/api/avatar?type=json";
        String result= HttpUtil.get(url1);
        var HeadPicJson=Getter.getHeadPic(result);
        Image localImage = new Image(HeadPicJson.getAvatar(), false);
        head.setImage(localImage);
    }

    @FXML
    void save_head(ActionEvent event) {
        Image image=head.getImage();
        saveImage(event,image);
    }

    @FXML
    void get_paper(ActionEvent event) {
        String url1="https://api.uomg.com/api/rand.img1?format=json";
        String result= HttpUtil.get(url1);
        var WallPaper=Getter.getWallPaper(result);
        Image localImage = new Image(WallPaper.getImgurl(), false);
        wallpaper.setImage(localImage);
    }

    @FXML
    void save_paper(ActionEvent event) {
        Image image=wallpaper.getImage();
        saveImage(event,image);
    }

    @FXML
    void get_view(ActionEvent event) {
        String url1="https://api.vvhan.com/api/view?type=json";
        String result= HttpUtil.get(url1);
        var Scenery=Getter.getScenery(result);
        Image localImage = new Image(Scenery.getImgurl(), false);
        view.setImage(localImage);
    }

    @FXML
    void save_view(ActionEvent event) {
        Image image=view.getImage();
        saveImage(event,image);
    }

    @FXML
    void id_search(ActionEvent event) {
        String num=id.getText();
        String url="https://widget.cifuwu.com/idcard/";
        String result= HttpUtil.get(url+num);
        var IdCard=Getter.getIdCard(result);
        id_text.setText(IdCard.getAll());
    }

    @FXML
    void domain_search(ActionEvent event) {
        String url1=domain.getText();
        String url="https://www.qqlykm.cn/api/who/?domain=";
        String result= HttpUtil.get(url+url1);
        int pre1 = result.indexOf("<pre>") + 6,pre2 = result.indexOf("</pre>",pre1);
        String pre = pre1 != 5&&pre2 != -1?result.substring(pre1,pre2):result;
        domain_text.setText(pre);

    }

    @FXML
    void saorao_search(ActionEvent event) {
        String num=saorao.getText();
        String url="https://qqlykm.cn/api/phone/phone.php?phone=";
        String result= HttpUtil.get(url+num);
        var BadPhoneInfo=Getter.getBadPhoneInfo(result);
        saorao_text.setText(BadPhoneInfo.getAll());
    }

    @FXML
    void guishudi_search(ActionEvent event) {
        try(ConnUtil connUtil = new ConnUtil()){
            connUtil.getConnect();
            String num=guishudi.getText();
            String s = connUtil.select("select local,type,bz from phone where phone = '" + num + "'");
            if(s == null){
                String url="https://api.vvhan.com/api/phone?tel=";
                String result= HttpUtil.get(url+num);
                var PhoneJson=Getter.getPhone(result);
                String str = PhoneJson.getInfo().getType();
                if(str.contains("<span")){
                    str = str.substring(0,str.indexOf("<span"));
                    PhoneJson.getInfo().setType(str);
                }
                connUtil.Insert("insert into phone(phone,local,type,bz) values('" + num + "','" + PhoneJson.getInfo().getLocal() + "','"
                        + PhoneJson.getInfo().getType() + "','" + PhoneJson.getInfo().getBz() + "')");
                guishudi_text.setText(PhoneJson.getAll());
            }else{
                guishudi_text.setText("电话号码:" + num + "\n" + s);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @FXML
    void tongyong_sec(ActionEvent event) {
        String path=getImagePath(event);
        Image localImage = new Image(path, false);
        tongyong1.setImage(localImage);
        System.out.println(path);
        String t=path.substring(6,path.length());
        System.out.println(t);
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
        String result=Ocr.recognition(url,t);
        System.out.println(result);
        var form = Getter.getWord(result);
        tongyong2.setText(form.getAll());
    }


    @FXML
    void jingque_sec(ActionEvent event) {
        String path=getImagePath(event);
        Image localImage = new Image(path, false);
        jingque1.setImage(localImage);
        System.out.println(path);
        String t=path.substring(6,path.length());
        System.out.println(t);
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic";
        String result=Ocr.recognition(url,t);
        System.out.println(result);
        var word = Getter.getWord(result);
        jingque2.setText(word.getAll());
    }


    @FXML
    void wangluo_sec(ActionEvent event) {
        String path=getImagePath(event);
        Image localImage = new Image(path, false);
        wangluo1.setImage(localImage);
        System.out.println(path);
        String t=path.substring(6,path.length());
        System.out.println(t);
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage";
        String result=Ocr.recognition(url,t);
        System.out.println(result);
        var word = Getter.getWord(result);
        wangluo2.setText(word.getAll());
    }


    @FXML
    void shouxie_sec(ActionEvent event) {
        String path=getImagePath(event);
        Image localImage = new Image(path, false);
        shouxie1.setImage(localImage);
        System.out.println(path);
        String t=path.substring(6,path.length());
        System.out.println(t);
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting";
        String result=Ocr.recognition(url,t);
        System.out.println(result);
        var word = Getter.getWord(result);
        shouxie2.setText(word.getAll());
    }


    @FXML
    void biaoge_sec(ActionEvent event) {
        String path=getImagePath(event);
        Image localImage = new Image(path, false);
        biaoge1.setImage(localImage);
        System.out.println(path);
        String t=path.substring(6,path.length());
        System.out.println(t);
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/form";
        String result=Ocr.recognition(url,t);
        System.out.println(result);
        var ocrForm = Getter.getOcrForm(result);
        biaoge2.setText(ocrForm.getAll());
    }



    @FXML
    void yinhang_sec(ActionEvent event) {
        String path=getImagePath(event);
        Image localImage = new Image(path, false);
        yinhang1.setImage(localImage);
        System.out.println(path);
        String t=path.substring(6,path.length());
        System.out.println(t);
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard";
        String result=Ocr.recognition(url,t);
        System.out.println(result);
        var BankCard = Getter.getBankCard(result);
        yinhang2.setText(BankCard.getAll());
    }


    String getImagePath(ActionEvent event) {
        Node source = (Node) event.getSource();
        Window theStage = source.getScene().getWindow();
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("View Pictures");
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))
        );
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("All Images", "*.*"),
                new FileChooser.ExtensionFilter("JPG", "*.jpg"),
                new FileChooser.ExtensionFilter("GIF", "*.gif"),
                new FileChooser.ExtensionFilter("BMP", "*.bmp"),
                new FileChooser.ExtensionFilter("PNG", "*.png")
        );
        File file = fileChooser.showOpenDialog(theStage);
        String localUrl="";
        try {
             localUrl = file.toURI().toURL().toString();
            return localUrl;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    void saveImage(ActionEvent event, Image image){
        FileChooser fileChooser1 = new FileChooser();
        fileChooser1.setTitle("Save Image");
        Node source = (Node) event.getSource();
        Window stage = source.getScene().getWindow();
        fileChooser1.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("JPG", "*.jpg"),
                new FileChooser.ExtensionFilter("GIF", "*.gif"),
                new FileChooser.ExtensionFilter("BMP", "*.bmp"),
                new FileChooser.ExtensionFilter("PNG", "*.png")
        );
        File file = fileChooser1.showSaveDialog(stage);
        if (file != null) {
            try {
                ImageIO.write(SwingFXUtils.fromFXImage(image,
                        null), "png", file);
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        Label label1 = new Label("美图发现");
        label1.setTextFill(Color.web("#000000"));
        tab1.setGraphic(label1);
        Label label2 = new Label("搜索工具");
        label2.setTextFill(Color.web("#000000"));
        tab2.setGraphic(label2);
        Label label3 = new Label("文字识别");
        label3.setTextFill(Color.web("#000000"));
        tab3.setGraphic(label3);
        String url1="https://api.vvhan.com/api/bing?type=json";
        String result= HttpUtil.get(url1);
        var OnePic=Getter.getOnePic(result);
        Image localImage = new Image(OnePic.getImgurl(), false);
        bing.setImage(localImage);
    }
}
